import { hashHistory } from 'dva/router';

export default {
    namespace: 'map',
    state: {
        text: '111111',
        mapPoints: [{
            id:'p00',
            longitude:116.468021,
            latitude:39.890092,
            markerContent: '<div>1111<div>'
        },{
            id:'p01',
            longitude:117.468021,
            latitude:39.890092,
            canShowLabel: true,
            config: {
                labelContent: '1dfasdf',
                isAnimation: true
            }
        },{
            id: 'car1',
            longitude:116.385827,
            latitude:39.913232
        }],
        mapLines: [{
            id: 'l1',
            paths: [[116.468021,39.890092],[117.468021,38.890092]],
            config: {
                isHidden: true
            }
        },{
            id: 'l2',
            paths: [[118.468021,37.890092],[119.468021,38.890092]],
            config: {lineWidth:4,color:'#fff'}
        }],
        mapPolygons:[{
            id: 'm1',
            rings: [[115.468021,38.890092],[117.468021,37.890092],[118.468021,39.890092]]
        },{
            id: 'm2',
            rings: [[116.468021,38.890092],[117.468021,39.890092],[118.468021,37.890092]]
        }],
        mapCircles:[{
            id: 'c1',
            longitude:116.468021,
            latitude:39.890092,
            radius: 10000,
        },{
            id: 'c2',
            longitude:117.468021,
            latitude:38.890092,
            radius: 10000,
        },],
        mapCenter: [117.468021,38.890092],
        setCenter: false,
        mapVisiblePoints:{fitView:'all',type:'all'},
        setVisiblePoints: false,
        mapCluster: [],
        setCluster: false,
        mapZoomLevel: 10,
        setZoomLevel: false,
        isRangingTool: false,
        editGraphicId: '',
        iseditGraphic: false,
        boundaryName: ['苏州'],
        mapDraw: {},
        isDraw: false,
        isClearAll: false
    },
    subscriptions:{
        setup({ dispatch, history }) {
            history.listen(location => {
            })
        }
    },
    effects:{
        *changeCenter({payload},{select,put,call}){
            yield put({
                type: 'isshowCenter',
                payload: {
                    setCenter: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isshowCenter',
                payload: {
                    setCenter: false
                }
            })
        },
        *changeFitview({payload},{select,put,call}){
            const {type} = payload;
            yield put({
                type: 'isshowFitview',
                payload: {
                    mapVisiblePoints: type || {fitView:'circle',type:'all'},
                    setVisiblePoints: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isshowFitview',
                payload: {
                    setVisiblePoints: false
                }
            })
        },
        *cluster({payload},{select,put,call}){
            let {mapPoints} = yield select(({map})=>map);
            yield put({
                type: 'isSetCluster',
                payload: {
                    mapCluster: mapPoints.map((item,index)=>{
                        return item.id;
                    }),
                    setCluster: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isSetCluster',
                payload: {
                    setCluster: false
                }
            })
        },
        *addPointAddFitview({payload},{select,put,call}){
            yield put({type: 'addPoint'});
            yield put({type: 'changeFitview',payload:{type:{fitView:'point',type:'center'}}});
        },
        *changeZoom({payload},{select,put,call}){
            let {mapZoomLevel} = yield select(({map})=>map);
            yield put({
                type: 'issetZoomLevel',
                payload: {
                    mapZoomLevel: mapZoomLevel>9?mapZoomLevel-1:mapZoomLevel+1,
                    setZoomLevel: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'issetZoomLevel',
                payload: {
                    setZoomLevel: false
                }
            })
        },
        *editRangingTool({payload},{select,put,call}){
            yield put({
                type: 'isRangingTool',
                payload: {
                    isRangingTool: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isRangingTool',
                payload: {
                    isRangingTool: false
                }
            })
        },
        *editGraphic({payload},{select,put,call}){
            yield put({
                type: 'iseditGraphic',
                payload: {
                    editGraphicId: payload.type,
                }
            });
        },
        *drawPoint({payload},{select,put,call}){
            let mapDraw = {
                geometryType: 'point',
                parameter: {},
                data: {id: 'point' + new Date().getTime()}
            };
            yield put({
                type: 'isDrawing',
                payload: {
                    mapDraw,
                    isDraw: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isDrawing',
                payload: {
                    isDraw: false
                }
            })
        },
        *drawPolyline({payload},{select,put,call}){
            let mapDraw = {
                geometryType: 'polyline',
                parameter: {},
                data: {id: 'polyline' + new Date().getTime()}
            };
            yield put({
                type: 'isDrawing',
                payload: {
                    mapDraw,
                    isDraw: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isDrawing',
                payload: {
                    isDraw: false
                }
            })
        }, 
        *drawPolygon({payload},{select,put,call}){
            let mapDraw = {
                geometryType: 'polygon',
                parameter: {},
                data: {id: 'polygon' + new Date().getTime()}
            };
            yield put({
                type: 'isDrawing',
                payload: {
                    mapDraw,
                    isDraw: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isDrawing',
                payload: {
                    isDraw: false
                }
            })
        }, 
        *drawCircle({payload},{select,put,call}){
            let mapDraw = {
                geometryType: 'circle',
                parameter: {},
                data: {id: 'circle' + new Date().getTime()}
            };
            yield put({
                type: 'isDrawing',
                payload: {
                    mapDraw,
                    isDraw: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isDrawing',
                payload: {
                    isDraw: false
                }
            })
        },
        *drawRectangle({payload},{select,put,call}){
            let mapDraw = {
                geometryType: 'rectangle',
                parameter: {},
                data: {id: 'rectangle' + new Date().getTime()}
            };
            yield put({
                type: 'isDrawing',
                payload: {
                    mapDraw,
                    isDraw: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'isDrawing',
                payload: {
                    isDraw: false
                }
            })
        },
        *clearAll({payload},{select,put,call}){
            yield put({
                type:'updateState',
                payload:{
                    isClearAll: true,
                    mapPoints: [],
                    mapLines: [],
                    mapPolygons: [],
                    mapCircles: [],
                    boundaryName: [],
                }
            });
            yield call(delay,1);
             yield put({
                type:'updateState',
                payload:{
                    isClearAll: false,
                }
            });
        }
    },
    reducers:{
        carTest(state,action){
            let {num} = action.payload;
            let {mapPoints} = state;
            mapPoints[2].longitude = test[num].lng;
            mapPoints[2].latitude = test[num].lat;
            return {...state,mapPoints};
        },
        updateState(state,action){
            return {...state,...action.payload};
        },
        /*点*/
        addPoint(state,action){
            let {mapPoints} = state;
            mapPoints = mapPoints.concat([{
                id:'10',
                longitude:116.468021,
                latitude:38.890092,
            },{
                id:'11',
                longitude:117.468021,
                latitude:37.890092,
                config: {width:50,height:12}
            }]);
            return {...state,mapPoints};
        },
        updatePoint(state,action){
            let {mapPoints} = state;
            mapPoints = mapPoints.map((item,index)=>{
                item.longitude += 1;
                item.latitude += 1;
                return item;
            })
            return {...state,mapPoints};
        },
        deletePoint(state,action){
            let {mapPoints} = state;
            mapPoints.splice(0,2);
            return {...state,mapPoints};
        },
        /*线*/
        addLine(state,action){
            let {mapLines} = state;
            mapLines = mapLines.concat([{
                id:'l3',
                paths: [[118.468021,37.890092],[119.468021,39.890092]],
                config: {lineWidth:4,color:'blue'}
            },{
                id:'l4',
                paths: [[117.468021,38.890092],[119.468021,36.890092]],
                config: {lineWidth:4,color:'yellow'}
            }]);
            return {...state,mapLines};
        },
        updateLine(state,action){
            let {mapLines} = state;
            mapLines[0].config = {lineWidth:4,color:'red'};
            return {...state,mapLines};
        },
        deleteLine(state,action){
            let {mapLines} = state;
            mapLines.splice(0,2);
            return {...state,mapLines};
        },
        /*面*/
        addPolygon(state,action){
            let {mapPolygons} = state;
            mapPolygons = mapPolygons.concat([{
                id: 'm3',
                rings: [[117.468021,37.890092],[116.468021,37.890092],[120.468021,39.890092]]
            },{
                id: 'm4',
                rings: [[119.468021,38.890092],[116.468021,39.890092],[114.468021,37.890092]]
            }]);
            return {...state,mapPolygons};
        },
        updatePolygon(state,action){
            let {mapPolygons} = state;
            mapPolygons[0].config = {color:'blue'};
            return {...state,mapPolygons};
        },
        deletePolygon(state,action){
            let {mapPolygons} = state;
            mapPolygons.splice(0,1);
            return {...state,mapPolygons};
        },
        /*圆*/
        addCircle(state,action){
            let {mapCircles} = state;
            mapCircles = mapCircles.concat([{
                id: 'c3',
                longitude:118.468021,
                latitude:38.890092,
                radius: 10000,
            },{
                id: 'c4',
                longitude:119.468021,
                latitude:39.890092,
                radius: 10000,
            }]);
            return {...state,mapCircles};
        },
        updateCircle(state,action){
            let {mapCircles} = state;
            mapCircles[0].config = {color:'blue'};
            return {...state,mapCircles};
        },
        deleteCircle(state,action){
            let {mapCircles} = state;
            mapCircles.splice(0,1);
            return {...state,mapCircles};
        },
        isshowCenter(state,action){
            return {...state,...action.payload};
        },
        isshowFitview(state,action){
            return {...state,...action.payload};
        },
        isSetCluster(state,action){
            return {...state,...action.payload};
        },
        issetZoomLevel(state,action){
            return {...state,...action.payload};
        },
        isRangingTool(state,action){
            return {...state,...action.payload};
        },
        iseditGraphic(state,action){
            return {...state,...action.payload};
        },
        editPoint(state,action){
            let {mapPoints} =state;
            const {id,longitude,latitude} = action.payload;
            mapPoints = mapPoints.map((item)=>{
                if(item.id == id){
                    item.longitude = longitude;
                    item.latitude = latitude;
                }
                return item;
            });
            return {...state,mapPoints};
        },
        editPolygon(state,action){
            let {mapPolygons} =state;
            const {id,rings} = action.payload;
            mapPolygons = mapPolygons.map((item)=>{
                if(item.id == id){
                    item.rings = rings;
                }
                return item;
            });
            return {...state,mapPolygons};
        },
        polyline(state,action){
            let {mapLines} =state;
            const {id,paths} = action.payload;
            mapLines = mapLines.map((item)=>{
                if(item.id == id){
                    item.paths = paths;
                }
                return item;
            });
            return {...state,mapLines};
        },
        addBoundary(state,action){
            let {boundaryName} =state;
            boundaryName.push('无锡');
            return {...state,boundaryName};
        },
        removeBoundary(state,action){
            let {boundaryName} =state;
            boundaryName.splice(0,1);
            return {...state,boundaryName};
        },
        isDrawing(state,action){
            return {...state,...action.payload};
        }
    }
}

//延迟
function delay(timeout){
  var pro = new Promise(function(resolve,reject){
    setTimeout(resolve, timeout);
  });
  return pro;
}



var test = [{"lng":116.380955,"lat":39.913349},{"lng":116.380776,"lat":39.913346},{"lng":116.380657,"lat":39.913344},{"lng":116.380488,"lat":39.913341},{"lng":116.380488,"lat":39.913341},{"lng":116.38014,"lat":39.913344},{"lng":116.379981,"lat":39.913342},{"lng":116.379624,"lat":39.913345},{"lng":116.379435,"lat":39.913342},{"lng":116.379276,"lat":39.913339},{"lng":116.378849,"lat":39.913331},{"lng":116.378848,"lat":39.913331},{"lng":116.378301,"lat":39.913571},{"lng":116.378272,"lat":39.91377},{"lng":116.378202,"lat":39.914189},{"lng":116.378141,"lat":39.914867},{"lng":116.378141,"lat":39.914867},{"lng":116.378509,"lat":39.914874},{"lng":116.379155,"lat":39.914946},{"lng":116.379393,"lat":39.91497},{"lng":116.380158,"lat":39.915024},{"lng":116.380218,"lat":39.915015},{"lng":116.380367,"lat":39.915018},{"lng":116.380427,"lat":39.915029},{"lng":116.380427,"lat":39.915029},{"lng":116.380447,"lat":39.91464},{"lng":116.380488,"lat":39.913341},{"lng":116.380498,"lat":39.913101},{"lng":116.380677,"lat":39.913114},{"lng":116.380677,"lat":39.913114},{"lng":116.381383,"lat":39.913127},{"lng":116.381442,"lat":39.913128},{"lng":116.381462,"lat":39.913128},{"lng":116.381601,"lat":39.913131},{"lng":116.381641,"lat":39.913131},{"lng":116.38183,"lat":39.913145},{"lng":116.381979,"lat":39.913147},{"lng":116.382407,"lat":39.913165},{"lng":116.382685,"lat":39.91317},{"lng":116.383112,"lat":39.913187},{"lng":116.383848,"lat":39.9132},{"lng":116.384106,"lat":39.913204},{"lng":116.384206,"lat":39.913206},{"lng":116.384624,"lat":39.913193},{"lng":116.385827,"lat":39.913232},{"lng":116.385877,"lat":39.913233},{"lng":116.386155,"lat":39.913238},{"lng":116.386364,"lat":39.913231},{"lng":116.386513,"lat":39.913234},{"lng":116.388134,"lat":39.913249},{"lng":116.388214,"lat":39.91324},{"lng":116.388433,"lat":39.913243},{"lng":116.388582,"lat":39.913245},{"lng":116.388632,"lat":39.913246},{"lng":116.388831,"lat":39.913249},{"lng":116.388911,"lat":39.91325},{"lng":116.389239,"lat":39.913245},{"lng":116.390254,"lat":39.913299},{"lng":116.394606,"lat":39.913453},{"lng":116.395064,"lat":39.913487},{"lng":116.395213,"lat":39.913499},{"lng":116.395403,"lat":39.913501},{"lng":116.395602,"lat":39.913503},{"lng":116.397227,"lat":39.913588},{"lng":116.398045,"lat":39.913614},{"lng":116.398164,"lat":39.913615},{"lng":116.398713,"lat":39.913659},{"lng":116.398992,"lat":39.913661},{"lng":116.399032,"lat":39.913662},{"lng":116.399292,"lat":39.913663},{"lng":116.399391,"lat":39.913664},{"lng":116.40009,"lat":39.913708},{"lng":116.400699,"lat":39.913702},{"lng":116.400988,"lat":39.913703},{"lng":116.401577,"lat":39.913706},{"lng":116.401887,"lat":39.913708},{"lng":116.402136,"lat":39.913709},{"lng":116.402796,"lat":39.913731},{"lng":116.403055,"lat":39.913742},{"lng":116.403994,"lat":39.913775},{"lng":116.405004,"lat":39.913816},{"lng":116.405833,"lat":39.913847},{"lng":116.405833,"lat":39.913847},{"lng":116.406393,"lat":39.913877},{"lng":116.406493,"lat":39.913877},{"lng":116.406733,"lat":39.913887},{"lng":116.406763,"lat":39.913887},{"lng":116.409504,"lat":39.914022},{"lng":116.409735,"lat":39.914022},{"lng":116.410466,"lat":39.914059},{"lng":116.410506,"lat":39.914069},{"lng":116.410816,"lat":39.914077},{"lng":116.410926,"lat":39.914077},{"lng":116.411167,"lat":39.914096},{"lng":116.411337,"lat":39.914095},{"lng":116.411998,"lat":39.914132},{"lng":116.412128,"lat":39.914131},{"lng":116.412359,"lat":39.91414},{"lng":116.412459,"lat":39.91415},{"lng":116.412629,"lat":39.914149},{"lng":116.412699,"lat":39.914148},{"lng":116.41291,"lat":39.914157},{"lng":116.41323,"lat":39.914165},{"lng":116.4133,"lat":39.914165},{"lng":116.413591,"lat":39.914183},{"lng":116.413681,"lat":39.914192},{"lng":116.413902,"lat":39.914191},{"lng":116.413942,"lat":39.9142},{"lng":116.41678,"lat":39.914217},{"lng":116.417231,"lat":39.914232},{"lng":116.417362,"lat":39.914231},{"lng":116.417693,"lat":39.914248},{"lng":116.417823,"lat":39.914247},{"lng":116.417944,"lat":39.914255},{"lng":116.418345,"lat":39.914261},{"lng":116.418435,"lat":39.91426},{"lng":116.418476,"lat":39.91426},{"lng":116.418626,"lat":39.914268},{"lng":116.418837,"lat":39.914266},{"lng":116.419158,"lat":39.914273},{"lng":116.421026,"lat":39.91431},{"lng":116.421287,"lat":39.914317},{"lng":116.421367,"lat":39.914316},{"lng":116.421468,"lat":39.914325},{"lng":116.421598,"lat":39.914323},{"lng":116.421699,"lat":39.914332},{"lng":116.4219,"lat":39.914339},{"lng":116.422563,"lat":39.91436},{"lng":116.422563,"lat":39.914359},{"lng":116.422655,"lat":39.912488},{"lng":116.422665,"lat":39.912358},{"lng":116.422665,"lat":39.912358},{"lng":116.423258,"lat":39.91238},{"lng":116.42379,"lat":39.912402},{"lng":116.424283,"lat":39.912425},{"lng":116.424494,"lat":39.912432},{"lng":116.424745,"lat":39.912479},{"lng":116.424745,"lat":39.912479},{"lng":116.424664,"lat":39.91303},{"lng":116.424654,"lat":39.91328},{"lng":116.424644,"lat":39.91342},{"lng":116.424634,"lat":39.91364},{"lng":116.424634,"lat":39.9137},{"lng":116.424614,"lat":39.91396},{"lng":116.424563,"lat":39.914401},{"lng":116.424543,"lat":39.914571},{"lng":116.424542,"lat":39.914571},{"lng":116.424512,"lat":39.914941},{"lng":116.424502,"lat":39.915211},{"lng":116.424502,"lat":39.915321},{"lng":116.424541,"lat":39.91584},{"lng":116.424551,"lat":39.91631},{"lng":116.424571,"lat":39.91655},{"lng":116.424581,"lat":39.91665},{"lng":116.424581,"lat":39.91674},{"lng":116.42466,"lat":39.917518},{"lng":116.42466,"lat":39.917698},{"lng":116.42466,"lat":39.918088},{"lng":116.42465,"lat":39.918188},{"lng":116.42465,"lat":39.918348},{"lng":116.42463,"lat":39.918848},{"lng":116.424389,"lat":39.918812},{"lng":116.424388,"lat":39.918812},{"lng":116.424388,"lat":39.918662}];